﻿<?php
	session_start();
	require_once('php/include/global.php');
	$user = new user();
	
	if(isset($_SESSION['user'])) {
		$user = unserialize($_SESSION['user']);
	}
	
	if(isset($_POST['login'])) {
		$email = $_POST['email'];
		$password = md5($_POST['password']); 
	
		if(!$user->login($email, $password)) {
			$errors['login'] = true;
		}
	}
	
	if(isset($_POST['logout'])) {
		$user->logout();
		header("Location: index.php");
	}
	
	ob_start();
	?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>PizzaWay</title>
        <link rel="stylesheet" type="text/css" href="static/css/default.css">
        <script language="JavaScript" src="static/js/jquery-1.3.2.min.js"></script>
 		<script language="JavaScript" src="static/js/ajax.js"></script>
		<script language="JavaScript" src="static/js/library.js"></script>
		<script language="JavaScript" src="static/js/ajax_framework.js"></script>
    </head>
    <body>
        <div id="container">
            <!--
            LOGIN AJAX - Temporaneamente disabilitato
            <div id="login">
            <?php 
			if($user->isLogged()) {
			?>
				<div id="login_content">
					Benvenuto, <?php echo $user->getUsername(); ?> [ <a href="#" onclick="logout()">logout</a> ]
				</div>
			<?php 
			} else {
			?>		
				<div id="login_content">
					<form method="post">
						email: <input type="text" name="email" id="email" />
						password: <input type="password" name="password" id="password" />
						<input type="button" id="ajaxLogin" value="login" onClick="login()" class="submit" />
					</form>
				</div>
				<?php
			}
			?>
            </div>
            -->
            
            <div id="login">
            <?php 
			if($user->isLogged()) {
			?>
				<div id="login_content">
					<form method="post" action="#">
						Benvenuto, <?php echo $user->getUsername(); ?>
						<input type="submit" id="logout" name="logout" value="logout" class="submit" />
					</form>
				</div>
			<?php 
			} else {
				// L'utente non è loggato e c'è un errore nel login -> ha inserito dei dati errati
				if(!isset($errors['login'])) {
			?>		
				<div id="login_content">
					<form method="post" action="#">
						email: <input type="text" name="email" id="email" />
						password: <input type="password" name="password" id="password" />

						<input type="submit" id="login" name="login" value="login" class="submit" />
					</form>
				</div>
			<?php
				} else {
			?>
				<div id="login_content">
						Siamo spiacenti, ma il nome utente e/o la password sono errati [ <a onclick="relogin()" onmouseover="this.style.cursor = 'pointer'"><u>Riprova</u></a> ]
				</div>
			<?php	
				}
			}
			?>
            </div>
            <div id="header">
            	
            </div>
            <!--Menu-->
            <div id="menu">
                <div id="navcontainer">
                    <ul id="navlist">
                        <li>
                            <a href="?p=home" <?php if($_GET['p'] == 'home') { echo "class='selected'"; } ?>>Home page</a>
                        </li>
                        <li>
                        	<a href="?p=shop" <?php if($_GET['p'] == 'shop') { echo "class='selected'"; } ?>>Shop</a>
                        </li>
						<?php 
						if(!$user->isLogged()) {
						?>
						<li id="reg">
                            <a href="?p=reg" <?php if($_GET['p'] == 'reg') { echo "class='selected'"; } ?>>Registrati</a>
                        </li>
                        <?php 
						}
                        ?>
                    </ul>
                </div>
            </div>
			<!--/Menu-->	
            <div id="containercenter">
                <div id="content">
                    <div id="navtrack">
            			<a href="index.php">Home</a> &raquo; <?php echo $_GET['p']; ?>
					</div>
		
					<?php
						if(!isset($_GET['p'])) { $_GET['p'] = 'home'; }
						
						switch($_GET['p']) {
							case 'home':
								echo '<h2>&raquo; Homepage</h2>';
									echo '<p>';
										include('php/pages/home.php');
									echo '</p>';
							break;
								
							case 'reg':
								echo '<h2>&raquo; Registrazione</h2>';
								echo '<p>';
									include('php/pages/registrazione.php');
								echo '</p>';
							break;
								
							case 'shop':
								echo '<h2>&raquo; Shop</h2>';
								echo '<p>';
									include('php/pages/shop.php');
								echo '</p>';
							break;
								
							case 'basket':
								echo '<h2>&raquo; Carrello</h2>';
								echo '<p>';
									include('php/pages/basket.php');
								echo '</p>';
							break;
							
							case 'clearBasket':
								if(isset($_SESSION['basket'])) {
									unset($_SESSION['basket']);								
								}
								echo '<h2>&raquo; Carrello</h2>';
								echo '<p>';
									include('php/pages/basket.php');
								echo '</p>';
							break;
								
							case 'order':
								echo '<h2>&raquo; Nuova Ordinazione</h2>';
								echo '<p>';
									include('php/pages/order.php');
								echo '</p>';
							break;
								
							case 'orderConfirm':
								echo '<h2>&raquo; Conferma ordinazione</h2>';
								echo '<p>';
									include('php/pages/order/confirm.php');
								echo '</p>';
							break;
							
							case 'orderStatus':
								echo '<h2>&raquo; Visualizza ordini</h2>';
								echo '<p>';
									include('php/pages/orderStatus.php');
								echo '</p>';
							break;
								
							case 'editPersonalData':
								echo '<h2>&raquo; Modifica dati personali</h2>';
								echo '<p>';
									include('php/pages/editPersonalData.php');
								echo '</p>';
							break;
								
							case 'editAddress':
								echo '<h2>&raquo; Modifica indirizzo</h2>';
								echo '<p>';
									include('php/pages/editAddress.php');
								echo '</p>';
							break;
								
							case 'newAddress':
								echo '<h2>&raquo; Nuovo indirizzo</h2>';
								echo '<p>';
									include('php/pages/newAddress.php');
								echo '</p>';
							break;
							
							case 'viewBonus':
								echo '<h2>&raquo; Elenco bonus disponibili</h2>';
								echo '<p>';
									include('php/pages/viewBonus.php');
								echo '</p>';
							break;

							case 'newIngredient':
								echo '<h2>&raquo; Nuovo ingrediente</h2>';
								echo '<p>';
									include('php/pages/newIngredient.php');
								echo '</p>';
							break;
							
							case 'editIngredient':
								echo '<h2>&raquo; Modifica ingrediente</h2>';
								echo '<p>';
									include('php/pages/editIngredient.php');
								echo '</p>';
							break;			
											
							case 'viewOrders':
								echo '<h2>&raquo; Visualizza ordini</h2>';
								echo '<p>';
									include('php/pages/viewOrders.php');
								echo '</p>';
							break;
							
							case 'newProduct':
								echo '<h2>&raquo; Nuovo prodotto</h2>';
								echo '<p>';
									include('php/pages/newProduct.php');
								echo '</p>';
							break;
							
							case 'userManagement':
								echo '<h2>&raquo; Gestione Utenti</h2>';
								echo '<p>';
									include('php/pages/userManagement.php');
								echo '</p>';
							break;
					
							case 'activate':
								echo '<h2>&raquo; Attivazione utente</h2>';
								echo '<p>';
									include('php/pages/activateUser.php');
								echo '</p>';
							break;
								
							default:							
								echo '<h2>&raquo; Pagina non trovata</h2>';
								echo '<p>';
									echo 'Siamo spiacenti, ma la pagina richiesta non &egrave; stata trovata';
								echo '</p>';
							break;
						}
					?>
                </div>
               <div id="rightbar">
               
                <?php
					include('php/pages/sidebar.php');
				?>
                	
                </div>
            </div>
			<!-- FOOTER -->
			<div id="footer">
				<a href="http://www.failbook.ch">PizzaWay</a> &copy; 2009 - David D'Agosta, Alessandro Fioroni, Elia Genini, Stefano Poncini, Nicol&ograve; Sartori
			</div>
        </div>
    </body>
<?php
	ob_flush();
?>
</html>
